package logger

import (
	"context"
	"fmt"
	"gorm.io/gorm/logger"
	"gorm.io/gorm/utils"
	"strconv"
	"time"
)

type ZapSqlLog struct {
	SqlLog Logger
}

func NewZapSqlLog(logger2 Logger) ZapSqlLog {
	return ZapSqlLog{
		SqlLog: logger2,
	}
}

func (s ZapSqlLog) LogMode(level logger.LogLevel) logger.Interface {
	fmt.Printf("level:%d", level)
	return nil
}

func (s ZapSqlLog) Info(c context.Context, msg string, data ...interface{}) {
	fmt.Printf("%v--%v--%v", msg, utils.FileWithLineNum(), data)
	//s.SqlLogger.Info("\n", args)
	//l.Printf(l.infoStr+msg, append([]interface{}{utils.FileWithLineNum()}, data...)...)
	s.SqlLog.Sql(1)
}

func (s ZapSqlLog) Warn(c context.Context, msg string, data ...interface{}) {
	fmt.Printf("%v--%v--%v", msg, utils.FileWithLineNum(), data)
	s.SqlLog.Sql(2)
}

func (s ZapSqlLog) Error(c context.Context, msg string, data ...interface{}) {
	fmt.Printf("%v--%v--%v", msg, utils.FileWithLineNum(), data)
	s.SqlLog.Sql(3)
}

func (s ZapSqlLog) Trace(c context.Context, begin time.Time, fc func() (sql string, rowsAffected int64), err error) {
	elapsed := time.Since(begin)
	sql, rows := fc()
	s.SqlLog.Sql(utils.FileWithLineNum(), strconv.Itoa(int(elapsed.Milliseconds()))+"ms", rows, sql)
}
